﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace a30_DialogBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MessageBoxClick(object sender, RoutedEventArgs e)
        {
            var text = "是否保存文档? Yes,保存. No,不保存. Cancel,放弃.";
            var caption = "保存";
            var result = MessageBox.Show(text, caption, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);

            //处理结果
            switch (result)
            {
                case MessageBoxResult.Yes:
                    MessageBox.Show("你选择了Yes", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case MessageBoxResult.No:
                    MessageBox.Show("你选择了No", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case MessageBoxResult.Cancel:
                    MessageBox.Show("你选择了Cancel", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
            }
        }

        private void OpenFileClick(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog
            {
                FileName = "Document",
                DefaultExt = ".txt",
                Filter = "plain text files (.txt)|*.txt"
            };

            // Open the dialog box modally
            var result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                var filename = dlg.FileName;
                MessageBox.Show("你选择了 " + filename, "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("无有效选择 ", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void SaveFileClick(object sender, RoutedEventArgs e)
        {
            var dlg = new SaveFileDialog
            {
                FileName = "Document",
                DefaultExt = ".txt",
                Filter = "plain text files (.txt)|*.txt"
            };

            // Open the dialog box modally
            var result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                var filename = dlg.FileName;
                MessageBox.Show("你选择了 " + filename, "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("无有效选择 ", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void PrintDocumentClick(object sender, RoutedEventArgs e)
        {
            var dlg = new PrintDialog
            {
                PageRangeSelection = PageRangeSelection.AllPages,
                UserPageRangeEnabled = true
            };

            // Open the dialog box modally
            var result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                MessageBox.Show("你选择了打印", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("你选择了不打印", "结果", MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }

        private void FindDialogClick(object sender, RoutedEventArgs e)
        {
            var dlg = new FindDialogBox(null) { Owner = this };

            // Open the dialog box modaless
            dlg.Show();

            //dlg.ShowDialog(); // Open the dialog box modally

        }

        private void FontDialogClick(object sender, RoutedEventArgs e)
        {
            var dlg = new FontDialogBox
            {
                Owner = this,
                FontFamily = documentTextBox.FontFamily,
                FontSize = documentTextBox.FontSize,
                FontWeight = documentTextBox.FontWeight,
                FontStyle = documentTextBox.FontStyle
            };

            // Configure the dialog box

            // Open the dialog box modally 
            dlg.ShowDialog();

            // Process data entered by user if dialog box is accepted
            if (dlg.DialogResult == true)
            {
                // Update fonts
                documentTextBox.FontFamily = dlg.FontFamily;
                documentTextBox.FontSize = dlg.FontSize;
                documentTextBox.FontWeight = dlg.FontWeight;
                documentTextBox.FontStyle = dlg.FontStyle;
            }
        }

        private void documentTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
